<template>
    <div class="dir-top-nowrap gap index-daily-recommendation">
        <div class="dir-left-nowrap cross-center" style="gap: 8px;">
            <img class="icon" src="/imgs/index/recommend.png">
            <div class="title">每日推荐</div>
        </div>
        <div class="dir-top-nowrap gap recommendation">
            <div class="box-grow-0 dir-left-nowrap tabs">
                <div ref="sliderRef" class="slider"></div>
                <div class="main-center cross-center tab-item" v-for="(item, index) in tabsList" :ref="tabItemRef.set"
                    @click="changeTab(index)" :class="[{ 'active': index === tabActiveIndex }]">
                    <div class="t-omit">{{ item.name }}</div>
                </div>
            </div>
            <div class="box-grow-1 list">
                <template v-for="(item, index) in tabsList" :key="item.name">
                    <div class="dir-top-nowrap gap tab-name" v-show="index === tabActiveIndex">
                        <template v-for="(item2) in item.list" :key="item2.id">
                            <div class="dir-left-nowrap cross-center gap item">
                                <div class="box-grow-1 title">
                                    {{ item2.title }}
                                </div>
                                <div class="box-grow-0 main-center cross-center like">
                                    <div class="txt">{{ item2.like }}</div>
                                </div>
                            </div>
                        </template>
                    </div>
                </template>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts" name="IndexDailyRecommendation">
/**
 * 首页每日推荐组件
 */

const sliderRef = ref<HTMLElement | null>(null)
const tabItemRef = useTemplateRefsList<HTMLDivElement>()
const tabActiveIndex = ref(0)
/**
 * 点击切换tab滑块到对应位置
 */
function changeTab(i: number) {
    tabActiveIndex.value = i
    const slider = sliderRef.value
    const tabItem = tabItemRef.value[i]
    if (slider && tabItem) {
        slider.style.left = tabItem.offsetLeft + 'px'
    }
}

const tabsList = [
    {
        name: 'COS',
        list: [
            {
                id: 1,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 2,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 3,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 4,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 5,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 6,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 7,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 8,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 9,
                title: '今夜有酒今夜醉',
                like: 100,
            },
            {
                id: 10,
                title: '今夜有酒今夜醉',
                like: 100,
            },
        ]
    },
    {
        name: '绘画',
        list: []
    },
    {
        name: '写作',
        list: []
    },
    {
        name: '日常',
        list: []
    }
]

</script>

<style lang='scss' scoped>
.index-daily-recommendation {
    .icon {
        width: 24px;
        height: 24px;
    }

    .title {
        width: 72px;
        height: 25px;
        font-size: 18px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #333333;
        line-height: 25px;
    }

    .recommendation {
        width: 440px;
        height: 540px;
        background: #FFFFFF;
        border-radius: 10px;
        padding: 20px 18px;

        .tabs {
            position: relative;
            height: 40px;
            border-radius: 10px;
            border: 1px solid #D8D8D8;

            .slider {
                position: absolute;
                left: 0;
                top: 0;
                width: calc(100% / 4);
                height: 100%;
                background: linear-gradient(180deg, #9DCBFF 0%, #C4E0F9 100%);
                border-radius: 10px;
                transition: left 0.3s ease-in-out;
            }

            .tab-item {
                position: relative;
                cursor: pointer;
                width: calc(100% / 4);
                padding: 0 10px;
                transition: color 0.3s ease-in-out;

                &.active {
                    color: #fff;
                }
            }
        }

        .list {
            padding: 0 20px;

            .item {
                .title {
                    font-size: 14px;
                    font-weight: 400;
                    color: #333333;
                }

                .like {
                    width: 62px;
                    height: 20px;
                    background: #F1F1F1;
                    border-radius: 4px;
                    font-size: 12px;
                    font-weight: 400;
                    color: #333333;
                    line-height: 12px;
                }

                &:nth-child(-n + 3) {
                    .title {
                        font-size: 16px;
                        font-weight: 500;
                        color: #333333;
                    }
                }

            }
        }
    }

}
</style>